/**
 * Created by chaos on 2017/7/9.
 */
import TransitComponent from '../../components/Transit/index.vue'

let $transit
const plugin = {
  install (vue, options) {
    const Transit = vue.extend(TransitComponent)
    if (!$transit) {
      $transit = new Transit({
        el: document.createElement('div')
      })
      document.body.appendChild($transit.$el)
    }
    // all Vux's plugins are included in this.$vux
    const transit = {
      showTransit: function (opt) {
        if (!opt.title) return false
        if (!opt.info) opt.info = ''
        if (!opt.url) opt.url = 'javascript:void(0)'
        if (!opt.type) opt.type = 'success'
        vue.$chaos.setTitle(opt.title)
        $transit.show = true
        $transit.opt = opt
      },
      success: function (opt) {
        opt.type = 'success'
        if (!opt.title) opt.title = '操作成功'
        return this.showTransit(opt)
      },
      warning: function (opt) {
        opt.type = 'warning'
        if (!opt.title) opt.title = '操作失败'
        return this.showTransit(opt)
      },
      notice: function (opt) {
        opt.type = 'notice'
        if (!opt.title) opt.title = '操作失败'
        return this.showTransit(opt)
      },
      hide: function () {
        $transit.show = false
      }
    }
    if (!vue.$chaos) {
      vue.$chaos = {
        transit
      }
    } else {
      vue.$chaos.transit = transit
    }

    vue.mixin({
      created: function () {
        this.$chaos = vue.$chaos
      }
    })
  }
}
export default plugin
export const install = plugin.install
